package cases

import (
  "github.com/gin-gonic/gin"
  "github.com/gin-gonic/gin/binding"
  "livefind/common/response"
  caseReq "livefind/service/cases/request"
  "livefind/utils"
  "livefind/utils/logger"
  "net/http"
)

type CaseApi struct {
}

// @Tags Cases
// @Summary 创建案件
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CaseCreateReq true "案件编号，案件名，调查员"
// @Success 200 {object} response.MsgResponse "{"code":20000,"message":"新建案件成功"}"
// @Router /cases/case [post]
func (ca *CaseApi) CreateCase(c *gin.Context) {
  var r caseReq.CaseCreateReq
  if err:=c.ShouldBindWith(&r,binding.JSON);err!=nil{
    c.AbortWithStatusJSON(
      http.StatusInternalServerError,
      gin.H{"error":err.Error()})
    logger.Error(err.Error(),err)
    return
  }

  if err := utils.Verify(r, utils.CreateCaseVerify); err != nil {
   logger.Error(err.Error(), err)
   response.FailWithMessage(err.Error(), c)
   return
  }

  err := caseService.Create(r, c)
  if err != nil {
    logger.Error(err.Error(), err)
    response.FailWithMessage(err.Error(), c)
    return
  }

  response.OkWithMessage("新建案件成功", c)
}

// @Tags Cases
// @Summary 根据条件获取案件列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.CaseListReq true "获取案件列表参数"
// @Success 200 {object} response.Response{data=response.CasesListRsp} "获取案件列表API响应"
// @router /cases/list [get]
func (ca *CaseApi) ListCases(c *gin.Context) {
  var r caseReq.CaseListReq
  if err:=c.ShouldBindWith(&r,binding.Query);err!=nil{
    c.AbortWithStatusJSON(
      http.StatusInternalServerError,
      gin.H{"error":err.Error()})
    logger.Error(err.Error(),err)
    return
  }

  rsp, err := caseService.List(r)
  if err != nil {
    logger.Error(err.Error(), err)
    response.FailWithMessage(err.Error(), c)
    return
  }

  response.OkWithDetailed(rsp, "获取案件列表成功", c)
}

// @Tags Cases
// @Summary 根据案件ID获取单条案件信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.CaseGetReq true "案件ID"
// @Success 200 {object} response.Response{data=response.CaseGetRsp,code=int,message=string} "根据案件ID获取单条案件信息API响应"
// @router /cases/case [get]
func (ca *CaseApi) GetCase(c *gin.Context) {
  var r caseReq.CaseGetReq
  if err:=c.ShouldBindWith(&r,binding.Query);err!=nil{
    c.AbortWithStatusJSON(
      http.StatusInternalServerError,
      gin.H{"error":err.Error()})
    logger.Error(err.Error(),err)
    return
  }

  if err := utils.Verify(r, utils.GetCaseVerify); err != nil {
    logger.Error(err.Error(), err)
    response.FailWithMessage(err.Error(), c)
    return
  }

  rsp, err := caseService.GetById(r)
  if err != nil {
    logger.Error(err.Error(), err)
    response.FailWithMessage(err.Error(), c)
    return
  }

  response.OkWithDetailed(rsp, "获取案件信息成功", c)
}
